package user

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
	"zhuandan/utils"
)

type FankuilistRBody struct {
	Data       []models.Fankui `json:"data"`
	PageNo     int             `json:"pageNo"`
	PageSize   int             `json:"pageSize"`
	TotalCount int64           `json:"totalCount"`
	TotalPage  int64           `json:"totalPage"`
}

type FankuilistBody struct {
	Page     int   `json:"pageNo" from:"pageNo"`
	Pagesize int   `json:"pageSize" from:"pageSize"`
	Uid      int64 `json:"uid" from:"uid"`
	Status   int64 `gorm:"column:status" json:"status"`
}

func Fankuilist(ctx *gin.Context) {
	request := FankuilistBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	status := request.Status
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Fankui
	res := data_source.Db.Where("uid = ?", uid)

	if status > 0 {
		res = res.Where("status = ?", status)
	}

	res.Model(&models.Fankui{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := FankuilistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

func Fankuiinfo(ctx *gin.Context) {
	request := FankuiaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	var users models.Fankui
	data_source.Db.Where("id = ?", id).First(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

type FankuiaddBody struct {
	ID       int64  `json:"id" gorm:"id"`
	Phone    string `json:"phone" gorm:"phone"`
	Uid      int64  `json:"uid" gorm:"uid"`
	Ddimage  string `json:"ddimage" gorm:"ddimage"`
	Zhimage  string `json:"zhimage" gorm:"zhimage"`
	Xsimage  string `json:"xsimage" gorm:"xsimage"`
	Title    string `json:"title" gorm:"title"` // 描述
	Luyin    string `json:"luyin" gorm:"luyin"`
	Orderids string `json:"orderids" gorm:"orderids"`
}

func Fankuiadd(ctx *gin.Context) {
	request := FankuiaddBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	phone := request.Phone
	ddimage := request.Ddimage
	zhimage := request.Zhimage
	title := request.Title
	luyin := request.Luyin
	orderids := request.Orderids
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    300,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请传递uid",
		})
		return
	}
	var fankuinum int64
	data_source.Db.Model(&models.Fankui{}).Where("phone = ?", phone).Count(&fankuinum)
	if len(orderids) > 0 {
		arr := strings.Split(orderids, ",")
		var fkorders []models.Fankuiorder
		data_source.Db.Where("orderid in (?)", arr).Find(&fkorders)
		if len(fkorders) > 0 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    300,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请勿提交重复订单",
			})
			return
		}
	}
	fk := models.Fankui{
		Uid:      uid,
		Phone:    phone,
		Ddimage:  ddimage,
		Zhimage:  zhimage,
		Title:    title,
		Addtime:  time.Now().Unix(),
		Status:   1,
		Luyin:    luyin,
		Ifchuci:  fankuinum,
		Orderids: orderids,
	}
	data_source.Db.Create(&fk)

	if len(orderids) > 0 {
		arr := strings.Split(orderids, ",")
		var fkorder []models.Fankuiorder
		for _, v := range arr {
			fko := models.Fankuiorder{
				Orderid:  utils.StrToInt64(v),
				Fankuiid: fk.ID,
			}
			fkorder = append(fkorder, fko)
		}
		data_source.Db.Create(&fkorder)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Ktkorder(ctx *gin.Context) {
	request := FankuiaddBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	dqtime := time.Now().Unix()
	qitianqian := dqtime - 86400*3

	var order []models.Procureorder
	data_source.Db.Where("status = 1").Where("addtime > ?", qitianqian).Where("uid = ?", uid).Find(&order)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    order,
		"msg":     "ok",
	})
	return

}

func Fankuiupdate(ctx *gin.Context) {
	request := FankuiaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	phone := request.Phone
	ddimage := request.Ddimage
	zhimage := request.Zhimage
	title := request.Title
	luyin := request.Luyin
	orderids := request.Orderids
	fk := models.Fankui{
		Phone:    phone,
		Ddimage:  ddimage,
		Zhimage:  zhimage,
		Title:    title,
		Addtime:  time.Now().Unix(),
		Status:   1,
		Luyin:    luyin,
		Orderids: orderids,
	}
	data_source.Db.Where("id = ?", id).Updates(&fk)

	data_source.Db.Where("fankuiid = ?", id).Delete(&models.Fankuiorder{})
	arr := strings.Split(orderids, ",")
	if len(orderids) > 0 {
		var fkorder []models.Fankuiorder
		for _, v := range arr {
			fko := models.Fankuiorder{
				Orderid:  utils.StrToInt64(v),
				Fankuiid: id,
			}
			fkorder = append(fkorder, fko)
		}
		data_source.Db.Create(&fkorder)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Fankuixs(ctx *gin.Context) {
	request := FankuiaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	xsimage := request.Xsimage
	fk := models.Fankui{
		Xsimage: xsimage,
		Status:  4,
	}
	data_source.Db.Where("id = ?", id).Updates(&fk)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type TishiRBody struct {
	Zhengju      string `json:"zhengju"`
	Zhengjuimage string `json:"zhengjuimage"`
	Dingdan      string `json:"dingdan"`
	Dingdanimage string `json:"dingdanimage"`
	Phonetitle   string `json:"phonetitle"`
}

func Tishi(ctx *gin.Context) {
	data := TishiRBody{
		Zhengjuimage: "http://zdb.suishilian.com/upload/zhengju.png",
		Dingdanimage: "http://zdb.suishilian.com/upload/dingdan.png",
		Zhengju:      "1、未接、拒接电话：通话记录截图需间隔1小时、10小时、20小时\n2、拨通后明确拒绝：将通话录音发至售后客服微信",
		Dingdan:      "请截全图，勿遮挡",
		Phonetitle:   "购买线索的手机号",
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
